package org.catdroid.bootcamp.server.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.catdroid.bootcamp.server.bean.Task;
import org.catdroid.bootcamp.server.bean.Tasks;
import org.catdroid.bootcamp.server.model.TaskModel;

import com.google.gson.Gson;

@SuppressWarnings("serial")
public class TaskServlet extends HttpServlet {
	@Override
	public void init() throws ServletException {
		// TODO Auto-generated method stub
		Task t=new Task();
		t.setId(1);
		t.setMessage("hola caracola");
		t.setTitle("saludo");
		TaskModel.addTask(t);
	}
	
	
	public void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws IOException {
		resp.setContentType("application/json");

		Tasks tasks=TaskModel.getTasks();
		
		Gson gson=new Gson();
		resp.getWriter().println(gson.toJson(tasks));
	}
	
	@Override
	protected void doPut(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		Task t= new Task();
		
		t.setId(Integer.parseInt(req.getParameter("id")));
		t.setTitle(req.getParameter("title"));
		t.setMessage(req.getParameter("message"));
		TaskModel.addTask(t);
		resp.getWriter().println("{\"status\":\"ok\"}");
	}

}
